<?php

// ------------------------------------------------------------------------
// |@Author       : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Date         : 2025-05-05 05:09:35
// |@----------------------------------------------------------------------
// |@LastEditTime : 2025-05-10 02:10:22
// |@----------------------------------------------------------------------
// |@LastEditors  : Jarmin <topextend@qq.com>
// |@----------------------------------------------------------------------
// |@Description  :
// |@----------------------------------------------------------------------
// |@FilePath     : ErrorCodeTrait.php
// |@----------------------------------------------------------------------
// |@Copyright (c) 2025 http://www.ladmin.cn All Rights Reserved.
// ------------------------------------------------------------------------
declare (strict_types=1);

namespace think\admin\traits;

/**
 * 错误码 Trait
 *
 * 编码规则：
 * 0        - 成功
 * 1-99     - 全局错误码
 * 1000-1999 - 用户权限相关错误
 * 2000-2999 - 业务逻辑相关错误
 * 3000-3999 - 第三方服务相关错误
 * 4000-4999 - 数据操作相关错误
 * 5000-5999 - 系统级别错误
 */
trait ErrorCodeTrait
{
    // 成功
    protected const SUCCESS = 0;

    // 全局错误 (100-199)
    protected const ERROR = 1;                    // 未知错误
    protected const PARAM_ERROR = 2;              // 参数错误
    protected const VALIDATE_ERROR = 3;           // 验证失败
    protected const REQUEST_METHOD_ERROR = 4;     // 请求方法错误
    protected const REQUEST_FORBIDDEN = 5;        // 请求被禁止
    protected const TOO_MANY_REQUESTS = 6;        // 请求频率过高
    protected const CLASS_NOT_EXISTS = 7;         // 类不存在
    protected const METHOD_NOT_EXISTS = 8;        // 方法不存在

    // 授权错误 (1000-1099)
    protected const AUTH_ERROR = 1000;            // 认证失败
    protected const TOKEN_INVALID = 1001;         // 令牌无效
    protected const TOKEN_EXPIRED = 1002;         // 令牌过期
    protected const TOKEN_REQUIRED = 1003;        // 需要令牌
    protected const TOKEN_MISSING = 1004;         // 令牌缺失
    protected const TOKEN_BLACKLIST = 1005;       // 令牌黑名单
    protected const PERMISSION_DENIED = 1006;     // 权限不足
    protected const SESSION_EXPIRED = 1007;       // 会话过期
    protected const ACCOUNT_DISABLED = 1008;      // 账号已禁用
    protected const ACCOUNT_NOT_EXIST = 1009;     // 账号不存在
    protected const PASSWORD_ERROR = 1010;        // 密码错误
    protected const LOGIN_ERROR = 1011;           // 登录失败

    // 用户相关 (1100-1199)
    protected const USER_NOT_FOUND = 1100;        // 用户不存在
    protected const USER_ALREADY_EXIST = 1101;    // 用户已存在
    protected const USER_NOT_ACTIVE = 1102;       // 用户未激活
    protected const USER_DISABLED = 1103;         // 用户已禁用
    protected const USER_INFO_ERROR = 1104;       // 用户信息错误

    // 角色权限 (1200-1299)
    protected const ROLE_NOT_FOUND = 1200;        // 角色不存在
    protected const ROLE_ALREADY_EXIST = 1201;    // 角色已存在
    protected const ROLE_DISABLED = 1202;         // 角色已禁用
    protected const ROLE_PERMISSION_DENIED = 1203;// 角色权限不足

    // 业务错误 (2000-2099)
    protected const BUSINESS_ERROR = 2000;        // 业务处理失败
    protected const DATA_NOT_FOUND = 2001;        // 数据不存在
    protected const DATA_ALREADY_EXIST = 2002;    // 数据已存在
    protected const DATA_UPDATE_FAILED = 2003;    // 数据更新失败
    protected const DATA_DELETE_FAILED = 2004;    // 数据删除失败
    protected const DATA_CREATE_FAILED = 2005;    // 数据创建失败
    protected const STATUS_ERROR = 2006;          // 状态错误

    // 文件操作 (2100-2199)
    protected const FILE_NOT_FOUND = 2100;        // 文件不存在
    protected const FILE_UPLOAD_FAILED = 2101;    // 文件上传失败
    protected const FILE_DELETE_FAILED = 2102;    // 文件删除失败
    protected const FILE_TYPE_ERROR = 2103;       // 文件类型错误
    protected const FILE_SIZE_ERROR = 2104;       // 文件大小错误
    protected const FILE_DOWNLOAD_FAILED = 2105;  // 文件下载失败

    // 第三方服务 (3000-3099)
    protected const API_REQUEST_FAILED = 3000;    // API请求失败
    protected const API_RESPONSE_ERROR = 3001;    // API响应错误
    protected const API_TIMEOUT = 3002;           // API超时
    protected const SMS_SEND_FAILED = 3003;       // 短信发送失败
    protected const EMAIL_SEND_FAILED = 3004;     // 邮件发送失败
    protected const PAYMENT_FAILED = 3005;        // 支付失败

    // 数据库操作 (4000-4099)
    protected const DB_ERROR = 4000;              // 数据库错误
    protected const DB_CONNECT_FAILED = 4001;     // 数据库连接失败
    protected const DB_QUERY_FAILED = 4002;       // 数据库查询失败
    protected const DB_TRANSACTION_FAILED = 4003; // 事务处理失败
    protected const DB_SAVE_FAILED = 4004;        // 数据保存失败
    protected const DB_DELETE_FAILED = 4005;      // 数据删除失败

    // 缓存操作 (4100-4199)
    protected const CACHE_ERROR = 4100;           // 缓存错误
    protected const CACHE_SET_FAILED = 4101;      // 缓存设置失败
    protected const CACHE_GET_FAILED = 4102;      // 缓存获取失败
    protected const CACHE_DELETE_FAILED = 4103;   // 缓存删除失败

    // 日志操作 (4200-4299)
    protected const LOG_ERROR = 4200;             // 日志错误
    protected const LOG_WRITE_FAILED = 4201;      // 日志写入失败
    protected const LOG_READ_FAILED = 4202;       // 日志读取失败

    // 配置操作 (4300-4399)
    protected const CONFIG_ERROR = 4300;          // 配置错误
    protected const CONFIG_NOT_FOUND = 4301;      // 配置不存在
    protected const CONFIG_SAVE_FAILED = 4302;    // 配置保存失败
    protected const CONFIG_LOAD_FAILED = 4303;    // 配置加载失败

    // 签名验证 (4400-4499)
    protected const SIGN_ERROR = 4400;            // 签名错误
    protected const SIGN_INVALID = 4401;          // 签名无效
    protected const SIGN_TIMEOUT = 4402;          // 签名超时

    // 参数验证 (4500-4599)
    protected const VALIDATE_FAILED = 4500;       // 参数验证失败

    // 系统错误 (5000-5099)
    protected const SYSTEM_ERROR = 5000;          // 系统错误
    protected const SERVICE_UNAVAILABLE = 5001;   // 服务不可用
    protected const SYSTEM_BUSY = 5002;           // 系统繁忙
    protected const SYSTEM_TIMEOUT = 5003;        // 系统超时
    protected const SYSTEM_MAINTENANCE = 5004;    // 系统维护中
    protected const SYSTEM_CONFIG_ERROR = 5005;   // 系统配置错误

    /**
     * 错误信息映射
     * @var array<int, string>
     */
    protected static array $messages = [
        self::SUCCESS => '操作成功',

        // 全局错误
        self::ERROR => '未知错误',
        self::PARAM_ERROR => '参数错误',
        self::VALIDATE_ERROR => '验证失败',
        self::REQUEST_METHOD_ERROR => '请求方法错误',
        self::REQUEST_FORBIDDEN => '请求被禁止',
        self::TOO_MANY_REQUESTS => '请求频率过高',

        // 授权错误
        self::AUTH_ERROR => '认证失败',
        self::TOKEN_INVALID => '无效的令牌',
        self::TOKEN_EXPIRED => '令牌已过期',
        self::TOKEN_REQUIRED => '需要令牌',
        self::TOKEN_MISSING => '令牌缺失',
        self::TOKEN_BLACKLIST => '令牌黑名单',
        self::PERMISSION_DENIED => '权限不足',
        self::SESSION_EXPIRED => '会话已过期',
        self::ACCOUNT_DISABLED => '账号已禁用',
        self::ACCOUNT_NOT_EXIST => '账号不存在',
        self::PASSWORD_ERROR => '密码错误',
        self::LOGIN_ERROR => '登录失败',

        // 用户相关
        self::USER_NOT_FOUND => '用户不存在',
        self::USER_ALREADY_EXIST => '用户已存在',
        self::USER_NOT_ACTIVE => '用户未激活',
        self::USER_DISABLED => '用户已禁用',
        self::USER_INFO_ERROR => '用户信息错误',

        // 角色权限
        self::ROLE_NOT_FOUND => '角色不存在',
        self::ROLE_ALREADY_EXIST => '角色已存在',
        self::ROLE_DISABLED => '角色已禁用',
        self::ROLE_PERMISSION_DENIED => '角色权限不足',

        // 业务错误
        self::BUSINESS_ERROR => '业务处理失败',
        self::DATA_NOT_FOUND => '数据不存在',
        self::DATA_ALREADY_EXIST => '数据已存在',
        self::DATA_UPDATE_FAILED => '数据更新失败',
        self::DATA_DELETE_FAILED => '数据删除失败',
        self::DATA_CREATE_FAILED => '数据创建失败',
        self::STATUS_ERROR => '状态错误',

        // 文件操作
        self::FILE_NOT_FOUND => '文件不存在',
        self::FILE_UPLOAD_FAILED => '文件上传失败',
        self::FILE_DELETE_FAILED => '文件删除失败',
        self::FILE_TYPE_ERROR => '文件类型错误',
        self::FILE_SIZE_ERROR => '文件大小错误',
        self::FILE_DOWNLOAD_FAILED => '文件下载失败',

        // 第三方服务
        self::API_REQUEST_FAILED => 'API请求失败',
        self::API_RESPONSE_ERROR => 'API响应错误',
        self::API_TIMEOUT => 'API超时',
        self::SMS_SEND_FAILED => '短信发送失败',
        self::EMAIL_SEND_FAILED => '邮件发送失败',
        self::PAYMENT_FAILED => '支付失败',

        // 数据库操作
        self::DB_ERROR => '数据库错误',
        self::DB_CONNECT_FAILED => '数据库连接失败',
        self::DB_QUERY_FAILED => '数据库查询失败',
        self::DB_TRANSACTION_FAILED => '事务处理失败',
        self::DB_SAVE_FAILED => '数据保存失败',
        self::DB_DELETE_FAILED => '数据删除失败',

        // 缓存操作
        self::CACHE_ERROR => '缓存错误',
        self::CACHE_SET_FAILED => '缓存设置失败',
        self::CACHE_GET_FAILED => '缓存获取失败',
        self::CACHE_DELETE_FAILED => '缓存删除失败',

        // 系统错误
        self::SYSTEM_ERROR => '系统错误',
        self::SERVICE_UNAVAILABLE => '服务不可用',
        self::SYSTEM_BUSY => '系统繁忙',
        self::SYSTEM_TIMEOUT => '系统超时',
        self::SYSTEM_MAINTENANCE => '系统维护中',
        self::SYSTEM_CONFIG_ERROR => '系统配置错误',
    ];

    /**
     * 获取错误信息
     * @param int $code 错误码
     * @param string $default 默认错误信息
     * @return string
     */
    protected static function getErrorMessage(int $code, string $default = ''): string
    {
        return static::$messages[$code] ?? ($default ?: '未知错误');
    }

    /**
     * 获取所有错误码和信息
     * @return array<int, string>
     */
    protected static function getAllErrors(): array
    {
        return static::$messages;
    }

    /**
     * 判断错误码是否存在
     * @param int $code 错误码
     * @return bool
     */
    protected static function hasError(int $code): bool
    {
        return isset(static::$messages[$code]);
    }
}
